package cn.leedsoft.and.crimeintent.controller.remote_control;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import cn.leedsoft.and.crimeintent.R;

/**
 * Created by leeds on 2016/8/23 9:40.
 */
public class RemoteControlFragment extends Fragment {
    private TextView mCurrentChannel;
    private TextView mInputChannel;
    public static RemoteControlFragment newInstance() {

        Bundle args = new Bundle();

        RemoteControlFragment fragment = new RemoteControlFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.remote_control_fragment, container, false);
        bindListeners(view);
        return view;
    }

    private void bindListeners(View view) {
        mCurrentChannel = (TextView) view.findViewById(R.id.remote_current_channel);
        mInputChannel = (TextView) view.findViewById(R.id.remote_input_channel);

        View.OnClickListener numBtnListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Button button = (Button)v;
                if(mInputChannel.getText().equals("0")) {
                    mInputChannel.setText(button.getText());
                } else {
                    mInputChannel.setText(mInputChannel.getText().toString().concat(button.getText().toString()));
                }
            }
        };
        TableLayout container = (TableLayout) view.findViewById(R.id.remote_container);
        int num = 0;
        for (int i = 2; i < container.getChildCount(); i++) {
            TableRow row = (TableRow) container.getChildAt(i);
            if (i != container.getChildCount() - 1) {//1~9数字键
                for (int j = 0; j < row.getChildCount(); j++) {
                    num++;
                    Button button = (Button) row.getChildAt(j);
                    button.setText(String.valueOf(num));
                    button.setOnClickListener(numBtnListener);
                }
            } else {
                Button delBtn = (Button) row.getChildAt(0);
                delBtn.setText("删除");
                delBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mInputChannel.setText("0");
                    }
                });
                Button zeroBtn = (Button) row.getChildAt(1);
                zeroBtn.setText("0");
                zeroBtn.setOnClickListener(numBtnListener);
                Button enterBtn = (Button) row.getChildAt(2);
                enterBtn.setText("确认");
                enterBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mCurrentChannel.setText(mInputChannel.getText());
                    }
                });
            }
        }

    }
}
